import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

import urllib
from google.appengine.api import urlfetch

import sys
import httplib
import re
import string

import datastore
from datetime import date

class Record(db.Model):

 def enumerate( self, start, finish):

   print("start = " + str(start) + " finish = " + str(finish ) )

   query="select __key__ from Standings order by idx"
   print query
   keys  = db.GqlQuery( query )

   count=-1
#  print( str(keys.count()) + " teams found ")
   for k in keys:
     t = db.get(k)
     print t.teamid, t.name

     count = count + 1
     if(count < start): continue
     if(count > finish): break
     print count,")",t.idx,t.teamid,t.name,"<br>"
     self.scrap(t.idx, t.teamid, t.name )

# -------------------------------------------------------------------
# Find and save the self-rated player to SelfRated database
 def scrap(self,team_idx,team_id,team_name):
    def Add(data):
      return ',"' + data + '"'

    url = "http://www.ustanorcal.com/teaminfo.asp?id="+ str(team_id)

    result = urlfetch.fetch(url)
    scraped = result.content

    rexp = r"href=playermatches\.asp\?id=([\d]*)>"    # id
    rexp += r"([\w'-]*)[ ,]([ \w'-]*)<[ \d\w\/<>=#]*>"   # LName, FName
    rexp += r"[ \d\w\/<>=#]*>"
    rexp += r"([\d\.\w]{3,4})"                        # NTRP
    rexp += r"[ \d\w<>\/=#.]*align=center>"
    rexp += r"([ \d]*)\/([ \d]*)"                     # Win/Loss


    r = re.compile(rexp,re.IGNORECASE )
    match = r.findall( scraped )
    for i,player in enumerate(match):
     uid = player[0]
     lname = player[1]
     fname = player[2]
     rating= player[3]
     win= player[4]
     loss= player[5]
     dominate = int(win)>int(loss)+0

#TODO  save team_idx, team_id, team_name (maybe?)

     if( rating == "3.5s" and (dominate or int(loss)==0) ):
#      print uid,lname,fname,rating,win,loss
       self.store( team_id,team_name, uid,lname,fname,rating,win,loss)
#      print(i,player) 


    return

 def store(self,team_id,team_name,uid,lname,fname,rating,win,loss):

    print(team_name, team_id,fname,lname,uid,win,loss )
    print("<br>")

    g = datastore.SelfRatePlayer( key_name= str(team_id) + str(uid) )
    g.ustaid = uid
    g.fname = fname.strip()
    g.lname = lname.strip()
    g.win = int(win.strip())
    g.loss = int(loss.strip())

    g.teamname = team_name
    g.teamid = team_id

    query="select __key__ from SelfRatePlayer where ustaid='"+uid+"'"
    keys  = db.GqlQuery( query )
    if( keys.count() >0):
      t = db.get(keys[0])
      t.loss = t.loss + 1
      r=t.put()
      print( "put" + str(t.teamid) + "=" + str(team_id) + "retval = " + str(r) )


#   Put into db with a single call
    db.put(g)


# ------------------------




class MainPage(webapp.RequestHandler):

  def Write(self,t):
    self.response.out.write(t)

  def get(self):
    start =  self.request.get('start') 
    finish =  self.request.get('finish')    
    if(start==""): start= "1"
    if(finish==""): start= "3"

    cup = Record()
    cup.enumerate(int(start),int(finish)  )

application = webapp.WSGIApplication(
                                     [('/record', MainPage)],
                                     debug=True)



def main():
    run_wsgi_app(application)

if __name__ == "__main__":

#   print __file__
#   print __name__

    main()
